<template>
  <div>
    <yfuTable ref="table" :param="commonTableParam.tableParam" v-on:getCheckedRows="getCheckedRows"></yfuTable>
    <yfuCommonTableDialog ref="dialog" :param="commonTableParam.dialogParam" v-on:fmModuleToRefreshTable="fmModuleToRefreshTable"></yfuCommonTableDialog>
  </div>
</template>

<script type="text/javascript">
  import yfuTable from '@/components/common/yfuTable.vue'
  import yfuCommonTableDialog from '@/components/common/yfuCommonTableDialog.vue'
  import { yfuErrorToast } from '@/utils/yfuMessage.js'
  import { yfuPost } from '@/utils/request.js'

  export default {
    props: ['commonTableParam'],
    components: {
      'yfuTable': yfuTable,
      'yfuCommonTableDialog': yfuCommonTableDialog
    },
    data () {
      return {
        dialogStatus: ''
      }
    },
    methods: {
      createModuleDialog () {
        this.dialogStatus = 'create'
        this.$refs.dialog.createModuleDialog()
      },
      modifyModuleDialog (row) {
        this.dialogStatus = 'update'
        this.$refs.dialog.modifyModuleDialog(row)
      },
      // 得到所有选中行
      getCheckedRows (row) {
        console.log(row)
      },
      refresh () {
        this.$refs.table.refreshTable()
      },
      // 删除
      clickDeleteButton () {
        let cThis = this

        // 获取选中行的数据
        if (cThis.$refs.table.getCheckedRows().length === 0) {
          yfuErrorToast('请至少选中一行')
        } else {
          cThis.$confirm('是否确认删除模块？', '删除模块', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let ids = []
            cThis.$refs.table.getCheckedRows().forEach(element => {
              console.log(element)
              ids.push(element.id)
            })

            let params = {}
            params.ids = ids.join()

            yfuPost(cThis.commonTableParam.tableParam.baseURL, cThis.commonTableParam.tableParam.deleteUrl, params,
              function (data) {
                console.log(data)
                cThis.refresh()
                console.log('删除成功!')
              },
              function (error) {
                console.log(error)
              }
            )
          }).catch(() => {
            cThis.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
        }
      },
      fmModuleToRefreshTable () {
        this.refresh()
      },
      getDialogStatus () {
        return this.dialogStatus
      }
    }
  }
</script>

<style>
</style>
